package com.ruoyi.ebike.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ebike.domain.Areas;
import com.ruoyi.ebike.service.IAreasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 骑行记录Controller
 * 
 * @author ruoyi
 * @date 2025-01-02
 */
@RestController
@RequestMapping("/activityConfigure/areas")
public class AreasController extends BaseController
{
    @Autowired
    private IAreasService areasService;

    /**
     * 查询区域列表
     */
    @PreAuthorize("@ss.hasPermi('activityConfigure:areas:list')")
    @GetMapping("/list")
    public TableDataInfo list(Areas areas)
    {
        startPage();
        List<Areas> list = areasService.selectAreasList(areas);
        if (list == null || list.isEmpty()) {
            System.out.println("list is empty or null");
            return getDataTable(new ArrayList<>());
        }

        return getDataTable(list);
    }

    /**根据部门id查询区域*/
    @PreAuthorize("@ss.hasPermi('activityConfigure:areas:query')")
    @GetMapping("last/{deptId}")
    public AjaxResult getAreasByDeptId(@PathVariable("deptId") Long deptId)
    {

        return success(areasService.selectAreasByDeptId(deptId));
    }

    /**
     * 获取行车区域详细信息
     */
    @PreAuthorize("@ss.hasPermi('activityConfigure:areas:query')")
    @GetMapping(value = "/{areaId}")
    public AjaxResult getInfo(@PathVariable("areaId") Long areaId)
    {
        return success(areasService.selectAreasByAreaId(areaId));
    }

    /**
     * 新增行车区域
     */
    @PreAuthorize("@ss.hasPermi('activityConfigure:areas:add')")
    @Log(title = "行车区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Areas areas)
    {
        return toAjax(areasService.insertAreas(areas));
    }

    /**
     * 修改行车区域
     */
    @PreAuthorize("@ss.hasPermi('activityConfigure:areas:edit')")
    @Log(title = "行车区域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Areas areas)
    {
        return toAjax(areasService.updateAreas(areas));
    }

}
